package grape.auth.rest;

import grape.auth.rest.auth2.email.EmailAuthenticationProvider;
import grape.auth.rest.auth2.mobile.MobileAuthenticationProvider;
import grape.auth.rest.auth2.wxmp.WxMpCodeAuthenticationProvider;
import grape.common.rest.CommonRestSecurityConfigure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * Created by yangwei
 * Created at 2020/1/13 14:19
 */
@Configuration
public class AuthRestSecurityConfig implements CommonRestSecurityConfigure {

    @Qualifier("accountUserDetailsService")
    @Autowired
    private UserDetailsService accountUserDetailsService;
    @Qualifier("wxMpCodeUserDetailsService")
    @Autowired
    private UserDetailsService wxMpCodeUserDetailsService;

    @Qualifier("mobileUserDetailsService")
    @Autowired
    private UserDetailsService mobileUserDetailsService;

    @Qualifier("emailUserDetailsService")
    @Autowired
    private UserDetailsService emailUserDetailsService;

    @Override
    public void configure(AuthenticationManagerBuilder auth,PasswordEncoder passwordEncoder)throws Exception {
        // 配置微信登录的处理器
        auth.authenticationProvider(wxMpCodeAuthenticationProvider());
        // 手机验证码登录处理器
        auth.authenticationProvider(mobileAuthenticationProvider());
        // 邮箱验证码登录处理器
        auth.authenticationProvider(emailAuthenticationProvider());
        auth.userDetailsService(this.accountUserDetailsService).passwordEncoder(passwordEncoder).configure(auth);


    }

    @Bean
    public WxMpCodeAuthenticationProvider wxMpCodeAuthenticationProvider()  {
        WxMpCodeAuthenticationProvider wxMpCodeAuthenticationProvider = new WxMpCodeAuthenticationProvider();
        wxMpCodeAuthenticationProvider.setUserDetailsService(this.wxMpCodeUserDetailsService);
        return wxMpCodeAuthenticationProvider;
    }


    @Bean
    public MobileAuthenticationProvider mobileAuthenticationProvider()  {
        MobileAuthenticationProvider frontUserMobileAuthenticationProvider = new MobileAuthenticationProvider();
        frontUserMobileAuthenticationProvider.setUserDetailsService(this.mobileUserDetailsService);
        return frontUserMobileAuthenticationProvider;
    }
    @Bean
    public EmailAuthenticationProvider emailAuthenticationProvider()  {
        EmailAuthenticationProvider frontUserMobileAuthenticationProvider = new EmailAuthenticationProvider();
        frontUserMobileAuthenticationProvider.setUserDetailsService(this.emailUserDetailsService);
        return frontUserMobileAuthenticationProvider;
    }
}
